package com.songcha.module_category.ui.fragment.category

import android.app.Application
import androidx.lifecycle.MutableLiveData
import com.songcha.library_base.interfaces.IApiResult
import com.songcha.library_base.mvvm.base.BaseViewModel
import com.songcha.library_business.bean.book.BookNetBean
import com.songcha.library_network.bean.BaseListDataBean
import com.songcha.module_category.bean.CategoryChannelListBean
import kotlin.random.Random

class CategoryViewModel(app: Application):BaseViewModel<CategoryRepository>(app) {
    var categoryList=MutableLiveData<MutableList<CategoryChannelListBean.ChannelBean>>()

    fun getCategoryChannelList(){
        handleApiDataObserver(repository!!.getCategoryChannelList(),object :IApiResult<CategoryChannelListBean>{
            override fun onSuccess(bean: CategoryChannelListBean) {
                categoryList.value=bean.data
            }

        },true)
    }

    fun getHomeRecommendDialogBook(block:(book: BookNetBean)->Unit){
        handleApiDataObserver(repository.getHomeRecommendDialogBookList(),object :IApiResult<BaseListDataBean<BookNetBean>>{
            override fun onSuccess(bean: BaseListDataBean<BookNetBean>) {
                val size = bean.data.size
                if(size == 0) return
                val r = Random(System.currentTimeMillis()).nextInt(size)
                block(bean.data[r])
            }

            override fun onError(e: Throwable) {

            }

        })
    }
}